/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CrawlerProcess.java
 *
 * Created on 23/11/2009, 11:47:35 PM
 */

package webcrawler;

/**
 *
 * @author cecv
 */
public class CrawlerProcess extends javax.swing.JFrame {

    private static final String SEARCH = "Search";
    private static final String STOP = "Stop";
    private static final String DISALLOW = "Disallow:";
    private static String newline="\n";
    private static String ContentType="text/html";
    private HiloCrawler searchThread;
    /** Creates new form CrawlerProcess */
    public CrawlerProcess() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lbl_url = new javax.swing.JLabel();
        lbl_tipo_contenido = new javax.swing.JLabel();
        lbl_titulo_resultado = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txt_resultado = new javax.swing.JTextArea();
        txt_url = new javax.swing.JTextField();
        txt_tipo_contenido = new javax.swing.JTextField();
        btn_detener = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        btn_buscar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(webcrawler.WebCrawlerApp.class).getContext().getResourceMap(CrawlerProcess.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setAutoscrolls(true);
        jPanel1.setName("jPanel1"); // NOI18N

        lbl_url.setText(resourceMap.getString("lbl_url_proceso.text")); // NOI18N
        lbl_url.setName("lbl_url_proceso"); // NOI18N

        lbl_tipo_contenido.setText(resourceMap.getString("lbl_tipo_contenido.text")); // NOI18N
        lbl_tipo_contenido.setName("lbl_tipo_contenido"); // NOI18N

        lbl_titulo_resultado.setText(resourceMap.getString("lbl_resultado_busqueda.text")); // NOI18N
        lbl_titulo_resultado.setName("lbl_resultado_busqueda"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txt_resultado.setColumns(20);
        txt_resultado.setRows(5);
        txt_resultado.setName("txt_resultado_busqueda"); // NOI18N
        jScrollPane1.setViewportView(txt_resultado);

        txt_url.setEditable(false);
        txt_url.setText(resourceMap.getString("txt_url_proceso.text")); // NOI18N
        txt_url.setName("txt_url_proceso"); // NOI18N

        txt_tipo_contenido.setEditable(false);
        txt_tipo_contenido.setText(resourceMap.getString("txt_tipo_contenido.text")); // NOI18N
        txt_tipo_contenido.setName("txt_tipo_contenido"); // NOI18N

        btn_detener.setText(resourceMap.getString("btn_detener.text")); // NOI18N
        btn_detener.setName("btn_detener"); // NOI18N
        btn_detener.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_detenerActionPerformed(evt);
            }
        });

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setForeground(resourceMap.getColor("jLabel4.foreground")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        btn_buscar.setText(resourceMap.getString("btn_buscar.text")); // NOI18N
        btn_buscar.setName("btn_buscar"); // NOI18N
        btn_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_buscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(jLabel4)
                .addContainerGap(300, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btn_detener)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lbl_tipo_contenido)
                                    .addComponent(lbl_url))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txt_url, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txt_tipo_contenido, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)))
                            .addComponent(btn_buscar))
                        .addGap(25, 25, 25))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(lbl_titulo_resultado)
                        .addGap(171, 171, 171)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_url)
                    .addComponent(txt_url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_tipo_contenido)
                    .addComponent(txt_tipo_contenido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btn_buscar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbl_titulo_resultado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btn_detener)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
   
    public void Imprimir(String resultado){

        this.txt_resultado.append(resultado+newline);
    }

  
      
    
    private void btn_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_buscarActionPerformed

      Imprimir("Buscando...");

	    // launch a thread to do the search
	    if (searchThread == null) {
		searchThread = new HiloCrawler("Hilo Crawler",this);
	    }
	    searchThread.start();
	
	
       
       

    }//GEN-LAST:event_btn_buscarActionPerformed

    private void btn_detenerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_detenerActionPerformed
        Imprimir("DETENER: Se esta deteniendo el proceso...");
        try
        {
            this.searchThread.stop();
            this.searchThread=null;
            Imprimir("DETENER: Se detuvo Satisfactoriamente");
        }
        catch(Exception e)
        {
            Imprimir("DETENER: Hubo problemas al detener el proceso - "+e.toString());
        }


    }//GEN-LAST:event_btn_detenerActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CrawlerProcess().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_buscar;
    private javax.swing.JButton btn_detener;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbl_tipo_contenido;
    private javax.swing.JLabel lbl_titulo_resultado;
    private javax.swing.JLabel lbl_url;
    public javax.swing.JTextArea txt_resultado;
    public javax.swing.JTextField txt_tipo_contenido;
    public javax.swing.JTextField txt_url;
    // End of variables declaration//GEN-END:variables

}
